﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Mechanics.GameEventMechanics {
    import Mechanics.GameMechanicInterfaces.*;
    import Mechanics.*;
    import Classes.*;
    import Classes.util.*;
    import Mechanics.Transactions.*;

    public class SlotMechanicImplementation implements ISlotMechanic {

        protected var m_owner:MechanicMapResource;
        protected var m_config:MechanicConfigData;
        protected var m_sendTransactions:Boolean;

        public function SlotMechanicImplementation(){
            this.m_sendTransactions = true;
        }
        public function get numSlots():int{
            return (this.m_config.params["numSlots"]);
        }
        public function get numEmptySlots():int{
            var _local3:int;
            var _local1:int;
            var _local2:Array = (this.m_owner.getDataForMechanic(this.m_config.type) as Array);
            if (_local2){
                _local3 = 0;
                while (_local3 < _local2.length) {
                    if (_local2[_local3] == null){
                        _local1++;
                    };
                    _local3++;
                };
            };
            return (_local1);
        }
        public function get numFilledSlots():int{
            return ((this.numSlots - this.numEmptySlots));
        }
        public function getSlot(_arg1:int){
            var _local2:String;
            var _local3:Array = (this.m_owner.getDataForMechanic(this.m_config.type) as Array);
            var _local4:Boolean = (((_arg1 >= 0)) && ((_arg1 < this.numSlots)));
            if (((_local4) && (_local3))){
                _local2 = _local3[_arg1];
            };
            return (_local2);
        }
        public function isValidData(_arg1):Boolean{
            var _local3:String;
            var _local4:Array;
            var _local5:String;
            var _local2:Item = Global.gameSettings.getItemByName(String(_arg1));
            if (_local2){
                _local3 = this.m_config.params["restrictByKeywords"];
                _local4 = _local3.split(",");
                for each (_local5 in _local4) {
                    if (_local2.itemHasKeyword(_local5)){
                        return (true);
                    };
                };
            };
            return (false);
        }
        public function fillNextSlot(_arg1):int{
            var _local4:int;
            var _local2 = -1;
            if (!this.isValidData(_arg1)){
                return (_local2);
            };
            var _local3:Array = (this.m_owner.getDataForMechanic(this.m_config.type) as Array);
            if (_local3){
                _local4 = 0;
                while (_local4 < _local3.length) {
                    if (this.canAdd(_local4, _local3, _arg1)){
                        _local3[_local4] = _arg1;
                        _local2 = _local4;
                        break;
                    };
                    _local4++;
                };
            };
            this.m_owner.setDataForMechanic(this.m_config.type, _local3);
            if (_local2){
                this.sendTransaction("fillNextSlot", _arg1);
            };
            return (_local2);
        }
        public function canAdd(_arg1:int, _arg2:Array, _arg3):Boolean{
            return ((_arg2[_arg1] == null));
        }
        public function isSlotFilled(_arg1:int):Boolean{
            return (!((this.getSlot(_arg1) == null)));
        }
        public function emptySlot(_arg1:int):Boolean{
            var _local2:Boolean;
            if (this.isSlotFilled(_arg1)){
                _local2 = this.setSlot(_arg1, null);
            };
            if (_local2){
                this.sendTransaction("emptySlot", _arg1);
            };
            return (_local2);
        }
        public function fillSlot(_arg1:int, _arg2):Boolean{
            var _local3:Boolean;
            if (this.isValidData(_arg2)){
                if (!this.isSlotFilled(_arg1)){
                    _local3 = this.setSlot(_arg1, _arg2);
                };
                if (_local3){
                    this.sendTransaction("fillSlot", _arg1, _arg2);
                };
            };
            return (_local3);
        }
        protected function setSlot(_arg1:int, _arg2):Boolean{
            var _local3:Boolean;
            var _local4:Array = (this.m_owner.getDataForMechanic(this.m_config.type) as Array);
            var _local5:Boolean = (((_arg1 >= 0)) && ((_arg1 < this.numSlots)));
            if (((_local5) && (_local4))){
                _local4[_arg1] = _arg2;
                _local3 = true;
            };
            this.m_owner.setDataForMechanic(this.m_config.type, _local4);
            return (_local3);
        }
        public function hasOverrideForGameAction(_arg1:String):Boolean{
            return ((this[_arg1] is Function));
        }
        public function executeOverrideForGameEvent(_arg1:String, _arg2:Array=null):MechanicActionResult{
            var _local3:MechanicActionResult = new MechanicActionResult(false, true);
            if ((this[_arg1] is Function)){
                _local3 = this[_arg1].apply(this, _arg2);
            };
            return (_local3);
        }
        public function getNumItems(_arg1:String):int{
            var _local4:String;
            var _local2:Array = (this.m_owner.getDataForMechanic(this.m_config.type) as Array);
            var _local3:int;
            for each (_local4 in _local2) {
                if (_local4 == _arg1){
                    _local3++;
                };
            };
            return (_local3);
        }
        public function initialize(_arg1:IMechanicUser, _arg2:MechanicConfigData):void{
            var _local4:int;
            this.m_owner = (_arg1 as MechanicMapResource);
            this.m_config = _arg2;
            var _local3:Array = (this.m_owner.getDataForMechanic(this.m_config.type) as Array);
            if (_local3 == null){
                _local3 = new Array();
                _local4 = 0;
                while (_local4 < this.numSlots) {
                    _local3[_local4] = null;
                    _local4++;
                };
            };
            this.m_owner.setDataForMechanic(this.m_config.type, _local3);
        }
        public function isVisitEnabled():Boolean{
            return (false);
        }
        public function set sendTransactions(_arg1:Boolean):void{
            this.m_sendTransactions = _arg1;
        }
        protected function sendTransaction(_arg1:String, ... _args):void{
            if (this.m_sendTransactions){
                GameTransactionManager.addTransaction(new TMechanicAction(this.m_owner, this.m_config.type, MechanicManager.ALL, {
                    operation:_arg1,
                    args:_args
                }));
            };
        }

    }
}//package Mechanics.GameEventMechanics 
